﻿@page "/stockin-detail/{Id:int}"
@using Microsoft.EntityFrameworkCore
@using Wms.Core.Entities
@using Wms.Core.Extensions
@inject Wms.Data.WarehouseDbContext Db
@inject NavigationManager Navigation

<div class="container-fluid py-4">
    <h4 class="text-primary mb-4">
        <span class="oi oi-data-transfer-down me-2"></span> 入库单明细
    </h4>

    @if (order == null)
    {
        <div class="text-center py-5">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">加载中...</span>
            </div>
            <p class="mt-3 text-muted">正在加载入库单数据...</p>
        </div>
    }
    else if (order == NotFound)
    {
        <div class="alert alert-danger">
            <strong>错误：</strong>未找到 ID 为 @Id 的入库单。
        </div>
        <button class="btn btn-secondary" @onclick="Back">
            <span class="oi oi-arrow-left me-1"></span> 返回
        </button>
    }
    else
    {
        <div class="row">
            <!-- 订单基本信息 -->
            <div class="col-12 mb-4">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="card-title mb-0">
                                <span class="badge bg-info text-dark">入库单</span>
                            </h5>
                            <small class="text-muted">ID: @order.Id</small>
                        </div>
                        <div class="row g-3">
                            <div class="col-md-6">
                                <div><strong>单号：</strong> <code>@order.OrderNo</code></div>
                            </div>
                            <div class="col-md-6">
                                <div><strong>日期：</strong> @order.Date.ToString("yyyy-MM-dd HH:mm")</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 明细表格 -->
            <div class="col-12">
                <div class="card shadow-sm">
                    <div class="card-header bg-white d-flex justify-content-between align-items-center">
                        <h6 class="mb-0">入库明细（共 @order.Details.Count 条）</h6>
                        <span class="badge bg-primary rounded-pill">@order.TotalQuantity()</span>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th scope="col">物料名称</th>
                                        <th scope="col" class="text-center">编码</th>
                                        <th scope="col" class="text-center">规格</th>
                                        <th scope="col" class="text-center">库位</th>
                                        <th scope="col" class="text-end">数量</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @if (order.Details.Any())
                                    {
                                        @foreach (var d in order.Details)
                                        {
                                            <tr>
                                                <td>
                                                    <strong>@d.Item?.Name</strong>
                                                </td>
                                                <td class="text-center text-muted">
                                                    @if (!string.IsNullOrEmpty(d.Item?.Code))
                                                    {
                                                        <code>@d.Item.Code</code>
                                                    }
                                                    else
                                                    {
                                                        <em class="text-secondary">无</em>
                                                    }
                                                </td>
                                                <td>
                                                    @d.Item?.Spec
                                                </td>
                                                <td class="text-center">
                                                    <span class="badge bg-info">@d.Location?.Name</span>
                                                </td>
                                                <td class="text-end">
                                                    <span class="badge bg-success">@d.Quantity</span>
                                                </td>
                                            </tr>
                                        }
                                    }
                                    else
                                    {
                                        <tr>
                                            <td colspan="3" class="text-center text-secondary py-4">
                                                <em>暂无入库明细</em>
                                            </td>
                                        </tr>
                                    }
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 返回按钮 -->
        <div class="mt-4">
            <button class="btn btn-outline-secondary px-4" @onclick="Back">
                <span class="oi oi-arrow-left me-1"></span> 返回列表
            </button>
        </div>
    }
</div>

@code {
    [Parameter] public int Id { get; set; }
    private StockInOrder? order;
    private const StockInOrder? NotFound = null; // 标记未找到

    protected override void OnInitialized()
    {
        order = Db.StockInOrders
            .Include(o => o.Details)
                .ThenInclude(d => d.Item)
            .Include(o => o.Details)
                .ThenInclude(d => d.Location)
            .FirstOrDefault(o => o.Id == Id);

        if (order == null)
        {
            order = NotFound; // 区分 null（加载中）和“未找到”
        }
    }

    private void Back()
    {
        Navigation.NavigateTo("/stockin-list");
    }
}